<template>
    <div class="box"
         :style="{
            'background': $store.state.nowTime==='moon' ? '#262525' : '#fff',
            'color': $store.state.nowTime==='moon' ? '#fff' : '#8f8c8c',
            'box-shadow': $store.state.nowTime==='moon' ?
                           '0 1px 1px 0 #fff, 0 2px 10px 0 #fff' :
                           '0 1px 1px 0 rgba(0, 0, 0, 0.2), 0 2px 5px 0 rgba(0, 0, 0, 0.19)',
            }">
        <div class="box_content"
             :style="{
            'background': $store.state.nowTime==='moon' ? '#262525' : '#fff',
            'color': $store.state.nowTime==='moon' ? '#fff' : '#8f8c8c'
            }">

            <textarea contenteditable="true" class="content"  ref="edit" @input="changeService" @focus="ess" @blur="ess_b">
            </textarea>
        </div>


      <div class="function">
         <div class="choose">
            <span @click="exp_s" ><img src="/img/common/biaoqing.png" alt="" title="表情" ></span>
            <button @click="send">发送评论</button>
         </div>




          <div class="expression" v-show="expression" >
              <ul >
                  <li @click="expression_li($event)" v-for="item in expression_e" v-html="item"></li>
              </ul>
          </div>
      </div>
    </div>
</template>




<script >
import api from "@/api";

export default {
    name: "CommentReplies",
    data(){
        return{
            expression:false,
            content:"勿喷，谢谢配合。",
            newComments:{
                uid:"",
                replyCid:"",
                replyUid:"",
                wid:"",
                platform:"",
                content:""
            },
            bulidIndex:0,
            essays:{
                wid:"",
                index:1,
            },
            communication:[{
              sender:"",
              recipient:"",
              message:"",
            }],
            expression_e:['😀', '😄', '😁', '😅', '🤣', '🙂', '😉', '😊', '🥰', '😍', '😘', '🤑',
                '😚', '😑', '😏', '😪', '😎', '😾', '😭', '😓', '😖', '👋', '🤬', '😷',
                '🤒', '🤕', '🤢', '🤮', '🤧', '😇', '🤠', '🤡', '🤥', '🤫', '🤭', '🧐',
                '🤓', '😈', '👿', '👹', '👺', '💀', '👻', '👽', '🤖', '💣', '💩', '😺',
                '😸', '😹', '😻', '😼', '😽', '🙀', '😿', ]
        }
    },
    mounted() {

    },
    methods: {
        expression_li(e){
            let text =  this.$refs.edit.value;
            this.$refs.edit.value= text.slice(0, this.bulidIndex) + e.target.innerText + text.slice(this.bulidIndex);
        },
        ess_b(e){
            this.bulidIndex = e.target.selectionStart;
        },
        ess() {
            if(this.$refs.edit.value === "")
            this.$refs.edit.value = "";
        },
        exp_s() {
            this.expression = !this.expression;
        },
        changeService() {
          this.$refs.edit.value;
        },
        send() {
            let user = JSON.parse(window.localStorage.getItem("user")); //获取用户id
            if(user==null){
                alert("请先登录")
               return;
            }else

           //更新第一页的评论
            if(this.$refs.edit.value === ""){
                alert("回复内容不能为空！")
                return;
            }
            let _this = this;
            let art = JSON.parse(window.localStorage.getItem("essayInfo"));
            this.essays.wid = art.wid //获取文章id
            this.newComments.wid = art.wid; //获取文章id
            this.newComments.replyUid = art.uid; //获取回复的用户id
            this.newComments.content = _this.$refs.edit.value; // 回复内容
            this.communication[0].sender = user.uid;
            this.communication[0].recipient = art.uid;
            this.communication[0].message = user.username+"评论您的文章: 《"+art.title+"》: "+_this.$refs.edit.value;
            this.newComments.platform = window.localStorage.getItem("platform"); //用户使用浏览器类型
            api.addComments(_this.newComments).then((res) => {
                res.data.code === 200 ? alert("评论成功") : alert("评论失败")
            })
            this.$emit("handleCommentSend")
            this.$refs.edit.value="";
            this.expression = false;

            //发送消息提示用户
            if(art.uid!==user.uid){
                api.communicationSet(this.communication)
            }

        },
    }
}
</script>

<style scoped>
.box{
    max-width: 50vw;
    min-width: 450px;
    margin: auto;
    margin-top: 20px;

}
.content{
    width: 100%;
    min-height: 20vh;
    color: #838080;
    padding: 10px;
    outline: none;
    resize: none;
    border: 1px solid rgba(138, 140, 145, 0.31) !important;
}

.box_content{
    padding: 10px;background: white;
    border-top-right-radius: 5px;
    border-top-left-radius: 5px;
}
.function{
    width: 100%;
    min-height: 50px;
    padding: 10px 20px;
}
.choose{
    display: flex;
    justify-content: space-between;
    align-items: center;

}
button{
    padding: 10px 13px;
    background: #3586f1;
    color: white;
    border:0;
}
button:hover{
    opacity: .9;
}
button:hover,img:hover{
    cursor: pointer;
}
.expression{
    width: 100%;
    min-height: 0;
    margin-top: 10px;
    padding: 5px;
    animation: box .5s ease-in-out 0s 1 forwards;
    border-radius: 4px;
}
.expression ul{
    display: flex;
    flex-flow:row wrap;
    justify-content: space-around;
    align-items: center;

}
.expression ul li{
    list-style: none;
    font-size: 28px;
}
.expression ul li:last-child{
    margin-right: auto !important;
}
.expression ul li:hover{
    cursor: pointer;
}
@keyframes box {
    0%{
        transform: scale(0);
    }
    70%{
        transform: scale(1.1);
    }
    100%{
        transform: scale(1);
    }
}
@media (min-width: 0) and (max-width: 600px) {
    .box{
        min-width: 380px !important;
    }
}
</style>